package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.SectionModel;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MJZJYTTokenizer extends BaseTokenizer {


    public MJZJYTTokenizer() {

    }

    private static MJZJYTTokenizer instance = new MJZJYTTokenizer();

    public static MJZJYTTokenizer getInstance() {
        return instance;
    }

    private static Logger logger = LoggerFactory.getLogger(MJZJYTTokenizer.class);

    public ExtractItem splitWords(ExtractBizModel extractBizModel, SectionModel sectionModel) {
        String text = sectionModel.getContent();
        IWord iWord = null;
        ExtractItem extractItem = null;
        List<IWord> wordList = splitWords(text);
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String value = iWord.getValue().trim();
            if (extractBizModel.getSynonyms().contains(value)) {
                extractItem = extractBizModel.getExtractItemBySynonym(value);
                logger.info("分析到募集资金用途相关信息，指标名称：{}，对应IteName：{}。", value, extractItem.getItemName());
                KVText keyText = new KVText();
                keyText.setText(extractItem.getItemName());
                KVText valueText = new KVText();
                valueText.setText(iWord.getValue());
                extractItem.setKey(keyText);
                extractItem.setValue(valueText);
                break;
            }
        }
        if (extractItem == null) {
            List<SectionModel> childSectionModelList = sectionModel.getChildSectionModelList();
            if (CollectionUtils.isEmpty(childSectionModelList)) {
                return null;
            }
            for (int i = 0; i < childSectionModelList.size(); i++) {
                SectionModel childSectionModel = childSectionModelList.get(i);
                if (childSectionModel == null) {
                    continue;
                }
                if (extractItem != null) {
                    break;
                }
                text = childSectionModel.getContent();
                if (org.apache.commons.lang3.StringUtils.isEmpty(text)) {
                    continue;
                }
                wordList = splitWords(text);
                for (int j = 0; j < wordList.size(); j++) {
                    iWord = wordList.get(j);
                    String value = iWord.getValue().trim();
                    if (extractBizModel.getSynonyms().contains(value)) {
                        extractItem = extractBizModel.getExtractItemBySynonym(value);
                        logger.info("分析到募集资金用途相关信息，指标名称：{}，对应IteName：{}。", value, extractItem.getItemName());
                        KVText keyText = new KVText();
                        keyText.setText(extractItem.getItemName());
                        KVText valueText = new KVText();
                        valueText.setText(iWord.getValue());
                        extractItem.setKey(keyText);
                        extractItem.setValue(valueText);
                        break;
                    }
                }
            }
        }
        return extractItem;
    }


    private List<IWord> splitWords(String text) {
        if (StringUtils.isEmpty(text)) {
            return Collections.emptyList();
        }
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> wordList = sentence.wordList;
        return wordList;
    }

}
